package org.java.dao.DaoImpl;

import org.java.dao.ArtGoodsDao;
import org.java.entity.Goods;
import org.java.util.JDBCUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

public class GoodsDaoImpl implements ArtGoodsDao {
    private JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtil.getDataSource());

    //获取mall页面的商品
    @Override
    public List<Map<String, Object>> mailInit(int page,int rows,String type,String size,String price) {

        int startIndex=(page-1)*rows;
        String sql="select * from goods where 1=1";
        //判断类型是否选择
        if(type!=null&&!type.equals("")){
            sql+=" and GoodsType like '%"+type+"%'";
        }
        if(size!=null&&!size.equals("")){
            switch (size){
                case "小于50cm":
                    sql+=" and GoodsSize<50";
                    break;
                case "50-100cm":
                    sql+=" and GoodsSize between 50 and 100 ";
                    break;
                case "大于150cm":
                    sql+=" and GoodsSize>150";
                    break;
            }
        }

        if(price!=null&&!price.equals("")){
            switch (price){
                case "0-10000元":
                    sql+=" and GoodsNewPrice<10000";
                    break;
                case "10000-20000元":
                    sql+=" and GoodsNewPrice between 10000 and 20000";
                    break;
                case "20000-300000元":
                    sql+=" and GoodsNewPrice between 20000 and 30000";
                    break;
                case "30000-50000元":
                    sql+=" and GoodsNewPrice between 30000 and 40000";
                    break;
                case "50000元以上":
                    sql+=" and GoodsNewPrice>50000";
                    break;
            }
        }

        sql+=" limit ?,?";

        return jdbcTemplate.queryForList(sql,startIndex,rows);

    }

    //获取商品总条数
    @Override
    public int goodCount(String type,String size,String price) {
        String sql="select count(*) from goods where 1=1";

        if(type!=null&&!type.equals("")){
            sql+=" and GoodsType like '%"+type+"%'";
        }

        if(size!=null&&!size.equals("")){
            switch (size){
                case "小于50cm":
                    sql+=" and GoodsSize<50";
                    break;
                case "50-100cm":
                    sql+=" and GoodsSize between 50 and 100 ";
                    break;
                case "大于150cm":
                    sql+=" and GoodsSize>150";
                    break;
            }
        }

        if(price!=null&&!price.equals("")){
            switch (price){
                case "0-10000元":
                    sql+=" and GoodsNewPrice<10000";
                    break;
                case "10000-20000元":
                    sql+=" and GoodsNewPrice between 10000 and 20000";
                    break;
                case "20000-300000元":
                    sql+=" and GoodsNewPrice between 20000 and 30000";
                    break;
                case "30000-50000元":
                    sql+=" and GoodsNewPrice between 30000 and 40000";
                    break;
                case "50000元以上":
                    sql+=" and GoodsNewPrice>50000";
                    break;
            }
        }

        int count = jdbcTemplate.queryForObject(sql, Integer.class);
        return count;
    }

    @Override
    public List<Goods> getGoodsList(int page,int rows) {
        int startIndex =(page-1)*rows;
        String sql="SELECT goodsPic,goodsName,goodsNewPrice,goodsOldPrice FROM goods limit ?,?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Goods.class),startIndex,rows);
    }

//    @Override
//    public List<Goods> getGoodsList() {
//        int startIndex =(page-1)*rows;
//        String sql="SELECT goodsPic,goodsName,goodsNewPrice,goodsOldPrice FROM goods limit";
//        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Goods.class),startIndex,rows);
//    }

    @Override
    public Integer goodCount() {
        String sql="select count(*) from goods";
        return jdbcTemplate.queryForObject(sql,Integer.class);
    }

    /*******获取*******/
    @Override
    public Goods getOneGoods(String goodsId) {
        String sql="select * from goods where GoodsId=?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(Goods.class),goodsId);
    }

    /****查询单个商品库存量*****/
    @Override
    public int queryCount(String gid) {
        String sql="select GoodsCount from goods where GoodsId=?";
        return jdbcTemplate.queryForObject(sql,Integer.class,gid);
    }

    /*****判断商品是否加入购物车*****/
    @Override
    public boolean addCart(String userId, String goodsId) {
        String sql="select count(*) from cart where UserId=? and GoodsId=?";
        Integer row = jdbcTemplate.queryForObject(sql, Integer.class, userId, goodsId);
        return row>0;
    }

    /*****增加新商品进购物车*****/
    @Override
    public void addGoodsInCart(String userId, String goodsId,int count) {
        String sql="insert into cart values(?,?,?)";
        jdbcTemplate.update(sql,userId,goodsId,count);
    }
}
